<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Sample</title>
<meta name="viewport"
            content="width=device-width, initial-scale=1.0">
<meta name="HandheldFriendly" content="true">
</head>
<body>
<canvas id="myCanvas" width="450" height="250"
          style="border:1px dashed #c3c3c3;">
   这里放置的内容用于浏览器不支持Canvas的情况
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
if (canvas && canvas.getContext) {
   var cxt = canvas.getContext("2d");
   // 创建image
   var img = new Image();
   img.src = "images/icon_ch.png";
   // 当图片加载完毕再绘制图片
   img.onload = function (event){
	  cxt.drawImage(img, 0, 0);
	  // ======================================================
	  // 首先获取ImageData对象，因为刚才画的图片的幅面是109*109
	  // 所以，指定幅面大小也是109*109
	  var imgd = cxt.getImageData(0, 0, 109, 109);
	  var pix = imgd.data;
	  
	  var imgd_r = cxt.createImageData(imgd); // 红色通道
	  var pix_r = imgd_r.data;
	  var imgd_g = cxt.createImageData(imgd); // 绿色通道
	  var pix_g = imgd_g.data;
	  var imgd_b = cxt.createImageData(imgd); // 蓝色通道
	  var pix_b = imgd_b.data;
	  var imgd_n = cxt.createImageData(imgd); // 拟合三个通道
	  var pix_n = imgd_n.data;
	  
	  // 遍历每个像素设置像素
	  var n = pix.length;
	  for (var i = 0; i < n; i += 4) {
	     pix_r[i  ] = pix[i  ]; // 仅包含红色通道的数据
	     pix_r[i+3] = pix[i+3];
	     pix_g[i+1] = pix[i+1]; // 仅包含绿色通道的数据
	     pix_g[i+3] = pix[i+3];
	     pix_b[i+2] = pix[i+2]; // 仅包含蓝色通道的数据
	     pix_b[i+3] = pix[i+3];
	     // 重新拟合各个通道的像素
	     pix_n[i  ] = pix_r[i  ];
	     pix_n[i+1] = pix_g[i+1];
	     pix_n[i+2] = pix_b[i+2];
		pix_n[i+3] = pix[i+3];
	  }
	  // 画ImageData对象
	  cxt.putImageData(imgd_r, 0, 109);
	  cxt.putImageData(imgd_g, 109, 109);
	  cxt.putImageData(imgd_b, 109*2, 109);
	  cxt.putImageData(imgd_n, 109*3, 109);
   }
}
</script>
</body>
</html>
